﻿using DataAccess;
using DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tools;

namespace WebUI.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private DataContext db = new DataContext();
        private UserLogin UL = new UserLogin();
        /// <summary>
        /// 菜单
        /// </summary>
        private IEntityRepository<SysMenu> MenuRepository;
        public HomeController()
        {
            MenuRepository = new EntityRepository<SysMenu>(db);
        }
        public ActionResult Index()
        {
           ViewBag.RealName= UserLogin.CreateUserInfo().RealName;
           var companyConfig = db.SysCompanyConfigs.FirstOrDefault();
           return View(companyConfig);
        }

        public ActionResult WorkSpace()
        {
            return View();
        }
        /// <summary>
        /// 为EasyUI Tree 提供异步数据
        /// </summary>
        /// <param name="id">加载当前结点的id值(node.id属性值),默认值为:0</param>
        /// <returns>Json格式的数据</returns>
        [HttpPost]
        public ActionResult MenuListForTree(int id = 0)
        {
            IEnumerable<SysMenu> Menus = Tools.UserLogin.GetLimitedMenu().Where(m => m.ParentID == id).ToList();
            List<Object> result = new List<Object>();
            foreach (var item in Menus)
            {
                result.Add(new { id = item.ID, text = item.Name, iconCls = item.IconCss, state = HasChild(item.ID) ? "closed" : "open", attributes = new { MenuUrl = item.MenuUrl, MenuAccess = Access(item.ID), MenuID = item.ID } });
            }
            return Json(result);
        }
        #region 其他
        /// <summary>
        /// 是否具有访问权限
        /// </summary>
        /// <param name="menuNo"></param>
        /// <returns></returns>
        private bool Access(int menuNo)
        {
            return true;
        }
        /// <summary>
        /// 判断当前id值是否存在子集
        /// </summary>
        /// <param name="id">当前id值</param>
        /// <returns></returns>
        private bool HasChild(int id)
        {

            return Tools.UserLogin.GetLimitedMenu().Any(m => m.ParentID == id);
        }

        #endregion
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
